package com.example.system.controller;

import com.example.common.util.Result;
import com.example.dto.system.SysUserRoleDto;
import com.example.system.Role;
import com.example.system.service.impl.RoleServiceImpl;
import com.example.system.service.impl.SysUserRoleServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Api(tags = "角色管理")
@RestController
@RequestMapping("admin/system/sysRole")
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;
    @Autowired
    private SysUserRoleServiceImpl sysUserRoleService;

//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result getPageList(@PathVariable("page") int page,
                              @PathVariable("limit") int limit,
//                              @RequestParam(value = "searchObj",required = false) String searchObj){
                              @RequestParam(value = "roleName",required = false) String searchObj){
        return roleService.getPageList(page, limit, searchObj);
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "获取")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable("id") Long id){
        return roleService.getById(id);
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "新增角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role){
        return roleService.insert(role);
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result update(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        if(!b) return Result.fail("修改失败");
        return Result.ok("添加成功");
    }

//    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id){
        if(id == null || id == 0L){
            return Result.fail("请输入id");
        }
        boolean b = roleService.removeById(id);
        if(!b) return Result.fail("删除失败");
        return Result.ok("删除成功");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
//    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id列表删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        Collection<Long> list = new ArrayList<>();
        for (Long id : ids){
            list.add(id);
        }
        boolean b = roleService.removeBatchByIds(list);
        if(!b) return Result.fail("删除失败");
        return Result.ok("删除成功");
    }

    @ApiOperation("获取用户的角色")
//    http://localhost:9528/dev-api/role/admin/system/sysRole/toAssign/1 GET
    @GetMapping("toAssign/{userId}")
    public Result getRoles(@PathVariable("userId") Long userId){
        return roleService.getRoles(userId);
    }

//    http://localhost:9528/dev-api/role/admin/system/sysRole/doAssign POST
    @PostMapping("doAssign")
    public Result doAssign(@RequestBody SysUserRoleDto dto){
        return roleService.doAssign(dto);
    }

}
